//
//  XZPunchCardFootView.m
//  Teecloud
//
//  Created by PC on 2018/11/29.
//  Copyright © 2018年 小志. All rights reserved.
//

#import "XZPunchCardFootView.h"
#import<SystemConfiguration/CaptiveNetwork.h>

@interface XZPunchCardFootView ()
@property (nonatomic, strong) ZJTimeCountDown *countDown;
@property (nonatomic, copy) NSString *wifiName;

@end
@implementation XZPunchCardFootView
- (void)awakeFromNib{
    [super awakeFromNib];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(clockTimeTapAction)];
    [self.circleView addGestureRecognizer:tap];
    
    _countDown = [[ZJTimeCountDown alloc]init];
    [self timeSecond];
    
    //获取wifi名称
    self.wifiName = [self wifiName];
    if (kIsEmptyString(self.wifiName)) {
        self.wifiImg.hidden = true;
        self.wifiNameLabel.hidden = true;
    }else{
        self.wifiImg.hidden = false;
        self.wifiNameLabel.hidden = false;
        self.wifiNameLabel.text = self.wifiName;
    }
}
- (void)setFormattedAddress:(NSString *)formattedAddress{
    _formattedAddress = formattedAddress;
    if (kIsEmptyString(formattedAddress)) {
        self.addressLabel.hidden = YES;
        self.addressImg.hidden = YES;
    }else{
        self.addressLabel.hidden = NO;
        self.addressImg.hidden = NO;
        self.addressLabel.text = formattedAddress;
    }
}
- (void)setModel:(XZWorkTimeInfo *)model{
    _model = model;
    self.nameLabel.text = [NSString stringWithFormat:@"%@打卡",model.Name];
}
/**
 当前时间正计
 */
- (void)timeSecond{
    //获取当前时间的时分秒
    NSDate *now = [NSDate date];
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSUInteger unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
    NSDateComponents *dateComponent = [calendar components:unitFlags fromDate:now];
    NSInteger hour =  [dateComponent hour];
    NSInteger minute =  [dateComponent minute];
    NSInteger second = [dateComponent second];
    
    NSInteger totalSecond = hour *3600 +minute*60+second;
    kWeakObject(self)
    [_countDown zj_timeCountUPWithCurrentTime:totalSecond interval:1 completeBlock:^(NSInteger hour, NSInteger minute, NSInteger second) {
        weakself.timeLabel.text = [NSString stringWithFormat:@"%02ld:%02ld:%02ld",(long)hour,(long)minute,(long)second];
    }];
}

/**
 获取链接的wifi名称

 @return wifi名称
 */
- (NSString *)wifiName {
    NSArray *ifs = CFBridgingRelease(CNCopySupportedInterfaces());
    id info = nil;
    for (NSString *ifname in ifs) {
        info = (__bridge_transfer id)CNCopyCurrentNetworkInfo((CFStringRef) ifname);
        if (info && [info count]) {
            break; }
        
    }
    NSDictionary *dic = (NSDictionary *)info;
    NSString *ssid = [[dic objectForKey:@"SSID"] lowercaseString];
    return ssid;
}
/**
 点击circleView区域进行打卡操作
 */
- (void)clockTimeTapAction{
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [dict setValue:FROM_IOS forKey:@"from"];
    [dict setValue:[XGMArchiverTools cacheForKey:BUSIORGID] forKey:@"busiOrgId"];
    [dict setValue:[XGMArchiverTools cacheForKey:USER_UserId] forKey:@"userId"];
    [dict setValue:[XGMArchiverTools cacheForKey:USER_identifierNumber] forKey:@"loginFinger"];
    
    [dict setValue:self.model.groupId forKey:@"groupId"];
    [dict setValue:self.model.TimeNo forKey:@"timeNo"];
    
    [dict setValue:self.longitude forKey:@"longitude"];
    [dict setValue:self.latitude forKey:@"latitude"];
    [dict setValue:self.formattedAddress forKey:@"gpsAddress"];
    [dict setValue:self.wifiName forKey:@"wifiName"];
    
    if (self.clockTimeBlock) {
        self.clockTimeBlock(dict);
    }
}
-(void)dealloc{
    [_countDown zj_timeDestoryTimer];
}
@end
